package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatUserGategory;
import com.happy.cat.project.system.service.ICatUserGategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 用户标签设定Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userGategory" )
@Api("用户标签设定 API")
public class CatUserGategoryController extends BaseController {

    private final ICatUserGategoryService iCatUserGategoryService;

    @GetMapping(value = "/all" )
    public AjaxResult all() {
        return AjaxResult.success(iCatUserGategoryService.list());
    }

    /**
     * 查询用户标签设定列表
     */
    @ApiOperation(value = "查询用户标签设定列表",notes = "查询用户标签设定列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userGategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatUserGategory catUserGategory)
    {
        startPage();
        LambdaQueryWrapper<CatUserGategory> lqw = new LambdaQueryWrapper<CatUserGategory>();
        if (StringUtils.isNotBlank(catUserGategory.getName())){
            lqw.like(CatUserGategory::getName ,catUserGategory.getName());
        }
        if (catUserGategory.getMoney() != null){
            lqw.eq(CatUserGategory::getMoney ,catUserGategory.getMoney());
        }
        if (catUserGategory.getDuration() != null){
            lqw.eq(CatUserGategory::getDuration ,catUserGategory.getDuration());
        }
        if (StringUtils.isNotBlank(catUserGategory.getRemarks())){
            lqw.eq(CatUserGategory::getRemarks ,catUserGategory.getRemarks());
        }
        if (catUserGategory.getCreateDate() != null){
            lqw.eq(CatUserGategory::getCreateDate ,catUserGategory.getCreateDate());
        }
        if (catUserGategory.getStatus() != null){
            lqw.eq(CatUserGategory::getStatus ,catUserGategory.getStatus());
        }
        List<CatUserGategory> list = iCatUserGategoryService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出用户标签设定列表
     */
    @ApiOperation(value = "导出用户标签设定列表",notes = "导出用户标签设定列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userGategory:export')" )
    @Log(title = "用户标签设定" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatUserGategory catUserGategory) {
        LambdaQueryWrapper<CatUserGategory> lqw = new LambdaQueryWrapper<CatUserGategory>(catUserGategory);
        List<CatUserGategory> list = iCatUserGategoryService.list(lqw);
        ExcelUtil<CatUserGategory> util = new ExcelUtil<CatUserGategory>(CatUserGategory. class);
        return util.exportExcel(list, "userGategory" );
    }

    /**
     * 获取用户标签设定详细信息
     */
    @ApiOperation(value = "获取用户标签设定详细信息",notes = "获取用户标签设定详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userGategory:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatUserGategoryService.getById(id));
    }

    /**
     * 新增用户标签设定
     */
    @ApiOperation(value = "新增用户标签设定",notes = "新增用户标签设定",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userGategory:add')" )
    @Log(title = "用户标签设定" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatUserGategory catUserGategory) {
        catUserGategory.setCreateDate(new Date());
        catUserGategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatUserGategoryService.save(catUserGategory) ? 1 : 0);
    }

    /**
     * 修改用户标签设定
     */
    @ApiOperation(value = "修改用户标签设定",notes = "修改用户标签设定",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userGategory:edit')" )
    @Log(title = "用户标签设定" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatUserGategory catUserGategory) {
        catUserGategory.setCreateDate(new Date());
        catUserGategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatUserGategoryService.updateById(catUserGategory) ? 1 : 0);
    }

    /**
     * 删除用户标签设定
     */
    @ApiOperation(value = "删除用户标签设定",notes = "删除用户标签设定",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userGategory:remove')" )
    @Log(title = "用户标签设定" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatUserGategoryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
